package com.hhuhh.smarthome.socket.model;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.hhuhh.smarthome.socket.IoConstants;
import com.hhuhh.smarthome.utils.ValidatorUtils;

/**
 * @author John zhang
 * @version 0.1
 */
public class SimpleData extends ModelWrapper {

	private static final long serialVersionUID = 1942736663291587090L;
	
	private static final String SUCCESS_KEY = "success";
	private static final String MESSAGE_KEY = "message";
	private static final String SINGLE_OBJECT_KEY = "obj";
	private static final String LIST_KEY = "list";
	
	private String action;
	private boolean success;
	private ArrayList<String> messages;
	private JSONObject singleObject;
	private JSONArray list;
	private String JSON;

	@Override
	public void parseResult() {
		String srcDataOfString = new String();
		
		if( getValue () instanceof byte[] ) {
			srcDataOfString = new String( (byte[]) getValue() );
		}
		else if( getValue() instanceof String ) {
			srcDataOfString = (String) getValue ();
		}
		
		action = parseActionName( srcDataOfString );
		if( action == null ) 
			return;
		
		int offset = srcDataOfString.indexOf ( IoConstants.ACTION_DATA_DELIMITER ) ;
		if( offset != -1 ) {
			this.JSON = srcDataOfString.substring ( offset + 1 );
		}
		
		if( ValidatorUtils.notEmpty ( JSON ) ) {
			transfromToJson ();
		}
		
		transfromToJson();
	}
	
	private String parseActionName( String result ) {
		if( result == null || result.equals ( "" ) )
			return null;
		
		int offset = result.indexOf ( IoConstants.ACTION_DATA_DELIMITER ) ;
		if( offset != -1 ) {
			return result.substring ( 0, offset ).trim ();
		}
		return null;
	}
	
	private void transfromToJson() {
		try {
			JSONObject obj = new JSONObject ( JSON );
			if ( obj.get ( SUCCESS_KEY ) != null ) {
				wrapperMessages ( JSON );
			}
			if ( obj.get ( LIST_KEY ) != null ) {
				wrapperLists ( JSON );
			}
			if ( obj.get ( SINGLE_OBJECT_KEY ) != null ) {
				wrapperSingleObject ( JSON );
			}
		} catch ( JSONException e ) {
			// do nothing
		}
	}
	
	private void wrapperMessages( String content ) {
		try {
			if( messages == null ) messages = new ArrayList<String>();
			JSONObject obj = new JSONObject ( content );
			this.success = obj.getBoolean ( SUCCESS_KEY );
			if( obj.getJSONArray ( MESSAGE_KEY ) != null ) {
				JSONArray arrays = obj.getJSONArray ( MESSAGE_KEY );
				for ( int i = 0; i < arrays.length (); i++ ) {
					messages.add ( arrays.getString ( i ) );
				}
			}
		} catch ( JSONException e ) {
			// do nothing
		}
	}

	private void wrapperLists( String content ) {
		try {
			JSONObject obj = new JSONObject ( content );
			this.list = obj.getJSONArray ( LIST_KEY );
		} catch ( JSONException e ) {
			e.printStackTrace ();
		}
	}

	private void wrapperSingleObject( String content ) {
		try {
			JSONObject obj = new JSONObject ( content );
			this.singleObject = obj.getJSONObject ( SINGLE_OBJECT_KEY );
		} catch ( JSONException e ) {
			e.printStackTrace ();
		}
	}
	
	public boolean isSuccess() {
		return success;
	}

	public ArrayList<String> getMessages() {
		return messages;
	}

	public JSONObject getSingleObject() {
		return singleObject;
	}

	public JSONArray getList() {
		return list;
	}
	
	public String getJSON() {
		return JSON;
	}

	public String getAction() {
		return action;
	}
}
